package com.sziit.se.highconcurrency.user.application.service.impl;

import com.sziit.se.highconcurrency.common.constant.SystemConstants;
import com.sziit.se.highconcurrency.common.entity.User;
import com.sziit.se.highconcurrency.common.entity.UserInfoVo;
import com.sziit.se.highconcurrency.common.service.IUserService;
import com.sziit.se.highconcurrency.common.tool.PasswordUtils;
import com.sziit.se.highconcurrency.user.application.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.UUID;

import static com.sziit.se.highconcurrency.common.constant.SystemConstants.SALT;

@Component
public class AdminAccountMaintain {

    @Autowired
    private UserMapper userMapper;


    /**
     * 应用启动后执行
     */
    @PostConstruct
    public void doCreateAdminAccount(){
        User user = userMapper.getByName(SystemConstants.ADMIN_ACCOUNT);
        if(null == user) {
            String userId = UUID.randomUUID().toString();
            String phone= SystemConstants.ADMIN_PHONE;
            String userName = SystemConstants.ADMIN_ACCOUNT;
            String role = SystemConstants.ADMIN_ROLE;

            //生成密码
            String passwordMd5 = PasswordUtils.generate(SystemConstants.ADMIN_PASSWORD, SALT);
            userMapper.insertUser(userId, userName, passwordMd5, phone, role);
        }
    }
}
